import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { AreaInfo, AreaItem, AreaItemFin } from './area-info'

@Component({
  selector: 'app-area-data-cn',
  templateUrl: './area-data-cn.component.html',
  styleUrls: ['./area-data-cn.component.scss']
})
export class AreaDataCnComponent implements OnInit {
  private areaInfo: AreaInfo;
  private _code: string = "-1"; // 初始化默认值
  private bInit: boolean = false;
  private bSet: boolean = false;
  public lists: AreaItemFin[][]; // 所有的区域
  public codes: string[]; // 选择的区域代码
  public araes: string[]; // 选择的区域中文

  @Output() codeChange: EventEmitter<string> = new EventEmitter<string>();
  @Output() areaChange: EventEmitter<string[]> = new EventEmitter<string[]>();

  @Input()
  public get code(): string {
    return this._code;
  }

  public set code(newCode: string) {
    if (this._code != newCode) {
      this._code = newCode;
      this.bSet = true;
      this.SelCode();
      this.bSet = false;
    }
  }

  constructor() {
    this.areaInfo = new AreaInfo();
    this.lists = new Array<AreaItemFin[]>(3);
    this.codes = new Array<string>(3);
    this.araes = new Array<string>(3);
  }

  ngOnInit() {
    this.ChangeSel(-1);
    this.bInit = true;
    this.SelCode();
  }

  // 修改下拉值
  public SelCode(): void {

    if (!this.bInit) return;
    // 代码不符合规范
    let cn = Number(this._code);
    if ((cn < 110000) || (cn > 999999)) {
      return;
    }

    if (cn % 10000 === 0) {
      // 一级
      this.codes[0] = String(cn);
      this.ChangeSel(0);
    }
    else if (cn % 100 === 0) {
      // 二级
      this.codes[0] = String(cn - (cn % 10000));
      this.ChangeSel(0);
      this.codes[1] = String(cn);
      this.ChangeSel(1);
    }
    else {
      // 三级
      this.codes[0] = String(cn - (cn % 10000));
      this.ChangeSel(0);
      this.codes[1] = String(cn - (cn % 100));
      this.ChangeSel(1);
      this.codes[2] = String(cn);
      this.ChangeSel(2);
    }
  }

  // 选择区域，重新渲染下拉框值
  public ChangeSel(idx: number): void {
    let c = ''; // 选择的选项 code

    if (idx >= 2) { // 县 、区处理
      if (this.lists[2] != undefined) {
        let item = this.lists[2].find((v, i, a) => v.n === this.codes[2]);
        if (item) {
          this.araes[2] = item.d;
          if (this.bInit && (this._code != this.araes[2])) {
            if (!this.bSet) {
              this._code = this.araes[2];
              this.codeChange.emit(this.codes[2]);
            }
            this.areaChange.emit(this.araes);
          }
        } else {
          this.araes[2] = "";
        }
      }

      return;
    } else if (idx < 0) {
      c = '86';
    } else {// 选择 市、县 处理
      c = this.codes[idx];
      if (this.lists[idx] != undefined) {
        let item = this.lists[idx].find((v, i, a) => v.n === c);
        if (item) {
          this.araes[idx] = item.d;
        }
        else if (this.lists[idx].length > 0) {
          this.araes[idx] = this.lists[idx][0].d;
        }
      }


      // 发射返回值
      if (idx == 1 && this.codes[idx] == "-1") { // 没有选择市，返回省
        this.codeChange.emit(this.codes[idx - 1]);
        this.areaChange.emit(this.araes);
      } else if (idx == 0 && this.codes[idx] == "-1") { // 没有选择省，返回 请选择
        this.codes[idx + 1] = "--请选择--";
        this.codeChange.emit(this.codes[idx]);
        this.areaChange.emit(this.araes);
      } else { // 选择 省、 市
        this.codeChange.emit(this.codes[idx]);
        this.areaChange.emit(this.araes);
      }
    }

    // 选择的数据 对象
    let a = this.areaInfo.data.find((v, i, a) => v.n === c);

    // 0 - 省， 1 - 市, 2 - 县
    let nextIdx = idx + 1;

    // 县级不加载，当nextIdx == 2 为县级 
    if (a && a.v && nextIdx != 2) {
      this.codes[nextIdx] = a.v[0].n;
      this.lists[nextIdx] = a.v;
      this.ChangeSel(idx + 1);
    }

  }
  reset() {
    this.codes[0] = "";
    this.codes[1] = "";
  }
}
